﻿namespace fpsrpg
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    #region Enumerations

    public enum AmmoType : byte
    {
        None = 0,
        Pistol = 1,
        AssaultRifle = 2,
        Smg = 3,
        Shotgun = 4,
        Rifle = 5,
        _Count = 6
    }

    #endregion Enumerations

    /// <summary>
    /// Representation of an ingame weapon
    /// </summary>
    public class Weapon
    {
        #region Fields

        /// <summary>
        /// Ammo currently in the currently used clip of the gun
        /// </summary>
        public uint AmmoInClip = 0;
        public AmmoType ammoType;

        /// <summary>
        /// Sound to be played when the a bullet from the weapon hits a sprite or terrain object
        /// </summary>
        public string BulletImpactSound;

        /// <summary>
        /// Cooldown time between shots
        /// </summary>
        public double cooldown = 0;
        public int Damage = 5;

        /// <summary>
        /// Maximum amount of ammo that can be present in the weapon at one time
        /// </summary>
        public uint MaxAmmo = 0;

        /// <summary>
        ///  The muzzleflash to be shown
        /// </summary>
        public Texture2D muzzleflash;

        /// <summary>
        /// The name of the texture of the bullet to be used
        /// </summary>
        public string WeaponBullet;

        /// <summary>
        /// The texture to shown in the players hud when wielding the weapon
        /// </summary>
        public Texture2D WeaponHudTexture;

        /// <summary>
        /// The audio asset to be played when the weapon is used
        /// </summary>
        public string WeaponSound;

        /// <summary>
        /// The in-game texture that is render 
        /// </summary>
        public Texture2D WeaponTexture;

        protected double timer;

        #endregion Fields

        #region Methods

        public virtual bool CanShoot()
        {
            return (AmmoInClip > 0 && timer > cooldown);
        }

        public virtual void Reload(uint bullets)
        {
            timer = -2f - cooldown;
               if (bullets < (MaxAmmo - AmmoInClip))
               AmmoInClip += bullets;
               else
               AmmoInClip = MaxAmmo;
        }

        public virtual int Shoot()
        {
            timer = 0f;
               AmmoInClip--;
               return Damage;
        }

        public virtual void SwitchedTo(Weapon from)
        {
            cooldown = (-this.cooldown -from.cooldown);
        }

        public virtual void Update(GameTime gt)
        {
            timer += (float)gt.ElapsedGameTime.TotalSeconds;
        }

        #endregion Methods
    }
}